package com.fingard.diagnostics.trace;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
//import java.util.concurrent.locks.Lock;
//import java.util.concurrent.locks.ReentrantLock;


import com.fingard.constant.Format;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;

/**按logCode取日志文件路径，按日期和文件大小分割*/
public class LogTraceFilePath {

	public int maxMsgLenEachFile = 2097152;//2097152--2M  //1048576--1M
	private TransIDHelper idHelper;
	private String defDir;
	private HashMap<String,String> dirMap;
	public LogTraceFilePath(String p_defDir){
		defDir = p_defDir;
		mapFile = new HashMap<String,TraceFileStruct>();
		dirMap = new HashMap<String,String>();
		//lock4map = new ReentrantLock();
		idHelper = new TransIDHelper(1, 99999);
	}
	
	/**增加日志目录配置，用于分目录写日志*/
	public void addLogDir(String pKey,String pDir){
		dirMap.put(pKey, pDir);
	}
	
	private HashMap<String,TraceFileStruct> mapFile;
	//private SpinLock lock = new SpinLock();
	//private Lock lock4map;
	public String getLogFilePath(String pDirKey, String pLogCode, String pPrefix, int p_msgLen){
		TraceFileStruct fileStruct;
		String fKey = pLogCode+"_"+pPrefix;
		if(mapFile.containsKey(fKey)){
			fileStruct = mapFile.get(fKey);
		}else{
			//lock4map.lock();
	        try {
	        	if(!mapFile.containsKey(fKey)){
	        		fileStruct = new TraceFileStruct();
	    			mapFile.put(fKey, fileStruct);
	        	}else{
	        		fileStruct = mapFile.get(fKey);
	        	}
	        } finally {
	        	//lock4map.unlock();
	        }
		}
		
		//为增加并发效率，这里的统计不进行并发控制，近似就可
		fileStruct.msgTotalLen += p_msgLen;
		String timeStep = Format.Date8Format.format(new Date());
        if ((fileStruct.msgTotalLen > maxMsgLenEachFile) || (!timeStep.equals(fileStruct.currentTimeStep))){
        	//yyyyMMdd--8位
    		//yyyyMMddHH--10位
        	String subPath;
    		if(timeStep.length() > 6){
    			subPath = timeStep.substring(0,6)+File.separator+timeStep.substring(6, timeStep.length());
    		}else{
    			subPath = timeStep;
    		}
        	fileStruct.filePath = getFilePath(pDirKey, subPath, pLogCode, pPrefix);
        	fileStruct.msgTotalLen = p_msgLen;
        	fileStruct.currentTimeStep = timeStep;
        	return fileStruct.filePath;
        }
        return fileStruct.filePath;
	}
	
	private String getFilePath(String pDirKey, String p_subPath, String pLogCode, String pPrefix){
		String mDir;
		String dKey = pLogCode+"_"+pPrefix;
		if(dirMap.containsKey(dKey)){
			mDir = dirMap.get(dKey);
		}else if(dirMap.containsKey(pLogCode)){
			mDir = dirMap.get(pLogCode);
		}else if(dirMap.containsKey(pDirKey)){
			mDir = dirMap.get(pDirKey);
		}else{
			mDir = defDir;
		}
		Date timeNow = new Date();
		String retPath = mDir + File.separator + p_subPath + File.separator + pLogCode;
		//File dir = new File(retPath);
		//if(!dir.exists()){
		//	dir.mkdirs();
		//}
		String fileNamePrefix = "";
		if(StringHelper.hasAnyChar(pPrefix)){
			fileNamePrefix = pPrefix+"_";
		}
		retPath = retPath+File.separator +fileNamePrefix+ Format.DateTime15Year2Format.format(timeNow) + idHelper.getNextIDStr()+".txt";
		return retPath;
	}
}
